<template>
  <div style="height:88%" ref="fromChart">
  </div>
</template>

<script>
import { getFrom } from '@/api'
export default {
  data() {
    return {
      setTimeId: '',
      fromChart: null,
      // echart中数据
      chartOption: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: []
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: []
      }
    }
  },
  methods: {
    // 基于准备好的dom，初始化echarts实例
    initChart() {
      this.fromChart = this.$echarts.init(this.$refs.fromChart)
      this.fromChart.setOption(this.chartOption)
    },
    // 获取表单数据
    getFrom() {
      getFrom().then(res => {
        if (res.data.code == 200) {
          res.data.series.forEach(res => {
            this.chartOption.legend.data.push(res.name)
          })
          this.chartOption.series = [...res.data.series]
          this.initChart()
          window.onresize = () => {
            this.setTimeId = setTimeout(() => {
              this.fromChart.resize()
            }, 10)
          }
        }
      })
    }
  },
  mounted() {
    this.getFrom()
  },
  beforeDestroy() {
    window.onresize = null
    this.setTimeId = null
  }
}
</script>

<style lang="scss" scoped>

</style>
